import { getGoodsBySelf } from "@src/api/mall/good";
import router from "@src/router";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import base from "@src/store/base";
import mall from "@src/store/mall";
import { MALL_RESERVATION_SHELF_ROUTER_PATH } from "@src/common/utils";
import { getPlanList, queryNearbyShopList } from "@src/api/reservation";
import location from "@src/store/location";
import { getStoreDetail } from "@src/api/mall/store";
const ROUTER_PATH = MALL_RESERVATION_SHELF_ROUTER_PATH;

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: mall,
      fields: ["storeId"],
    },
    {
      store: location,
      fields: ["latitude", "longitude"],
      actions: ["getLocation"],
    },
  ],
  /**
   * 组件的属性列表
   */
  properties: {
    goodsBillboard: {
      type: Array,
      observer: function (newVal, oldVal) {
        // 属性值变化时执行
        if (this.data.storeId) {
          this.setData({
            goodsList: [],
            pageNum: 1,
            noData: false,
          });
          console.log("执行1");
          this.getGoodsBySelfFunc();
        }
      },
    },
    //橱窗类型  liebiaochuchuang：列表橱窗  gundongchuchuang：滚动橱窗
    boardType: {
      type: String,
      value: "liebiaochuchuang",
    },
    login: {
      type: Boolean,
    },
  },
  /**
   * 组件生命周期
   */
  lifetimes: {
    attached: function (val) {
      //goodsBillboard 商品橱窗中只有一个模块
      // console.log("this.data.goodsBillboard", this.data.goodsBillboard);
      if (this.data.goodsBillboard[0] && this.data.goodsBillboard[0].biz) {
        let biz = this.data.goodsBillboard[0].biz;
        if (biz.type === "MALL_SHELF" || biz.type === "RESERVATION_SHELF") {
          let params = JSON.parse(biz.params);
          this.setData({
            shelfId: params.shelfId,
            goodsList: [],
            bizType: biz.type,
          });
          this.updateStoreBindings();
          this.getGoodsBySelfFunc();
        }
      }
    },
    ready() {},
  },
  observers: {
    storeId: function (storeId) {
      if (storeId) {
        this.getStoreDetail();
      }
    },
    latitude: function () {
      if (this.data.bizType === "RESERVATION_SHELF") {
        this.queryNearbyShopListReq();
      }
    },
    // longitude: function () {
    //   if (this.data.bizType === "RESERVATION_SHELF") {
    //     this.queryNearbyShopListReq();
    //   }
    // },
  },
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的初始数据
   */
  data: {
    shelfId: null,
    bizType: null,
    pageNum: 1,
    pageSize: 9,
    goodsList: [],
    noData: false,
    nearbyShopList: [],
    storeInfo: {},
  },
  /**
   * 组件的方法列表
   */
  methods: {
    queryNearbyShopListReq() {
      if (!this.data.login) {
        // this.triggerEvent("login");
        return;
      }
      if (this.data.nearbyShopList.length > 0) return;
      let data = {
        latitude: this.data.latitude || "",
        longitude: this.data.longitude || "",
      };
      return queryNearbyShopList(data).then((res) => {
        if (res.code === 0) {
          console.log(
            "app-shangpinchuchuang获取预约门店列表==============================",
            res.data
          );
          this.setData({ nearbyShopList: res.data });
        }
      });
    },
    getStoreDetail() {
      getStoreDetail({ storeId: this.data.storeId }).then(async (res) => {
        this.setData({ storeInfo: res.data });
        if (this.data.nearbyShopList.length === 0) {
          // await this.queryNearbyShopListReq();
        }
      });
    },
    //加载橱窗商品
    getGoodsBySelfFunc() {
      let self = this;
      // if (!this.data.hasNextPage) {
      //   return;
      // }
      if (this.data.noData) {
        return;
      }
      let storeId = this.data.storeId;
      let req = {
        shelfId: this.data.shelfId,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
      };
      const api =
        this.data.bizType === "RESERVATION_SHELF"
          ? getPlanList
          : getGoodsBySelf;

      api(storeId, req).then((res) => {
        let list = self.data.goodsList.concat(res.data.list);
        let noData = !res.data.hasNextPage;
        self.setData({
          goodsList: list,
          pageNum: req.pageNum + 1,
          noData: noData,
        });
      });
    },
    loadImages() {
      console.log("触底了");
    },
    //跳商品详情页
    jumpPage(e) {
      if (!this.data.login) {
        this.triggerEvent("login");
        return;
      }
      let item = e.currentTarget.dataset.item;
      let urlParams = {
        goodsSerial: item.goodsSerial,
        storeId: this.data.storeId,
      };
      if (this.data.bizType === "RESERVATION_SHELF") {
        const { storeNo } = this.data.storeInfo;
        const nearbyShop =
          this.data.nearbyShopList.filter((v) => v.storeNo === storeNo)[0] ||
          this.data.nearbyShopList[0];
        console.log(
          "app-shangpinchuchuang跳转前",
          nearbyShop,
          this.data.storeInfo
        );
        urlParams.id = item.planId;
        urlParams.planId = item.planId;
        urlParams.shopId = nearbyShop.shopId || 0;
        urlParams.shopConfigId = nearbyShop.shopConfigId || 0;
      }
      router.push({ name: ROUTER_PATH[this.data.bizType], params: urlParams });
    },
  },
});
